<template>
  <PageWrapper>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="customHeader"> 导出表格 </a-button>
      </template>
    </BasicTable>
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { BasicTable, useTable } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { getBasicColumns, searchFormSchema } from './orderStatisticsCarData';
  import { getCarOrderByParams } from '/@/api/orderStaiistics/car/carOrderStaiisticsAPI';
  import { jsonToSheetXlsx } from '/@/components/Excel';
  import { toRaw, ref } from 'vue';
  import dayjs from 'dayjs';
  export default defineComponent({
    components: { BasicTable, PageWrapper },
    setup() {
      const [registerTable, { getDataSource }] = useTable({
        api: getCarOrderByParams,
        title: '专车订单统计',
        columns: getBasicColumns(),
        formConfig: {
          labelWidth: 100,
          actionColOptions: { span: 24 },
          schemas: searchFormSchema,
          fieldMapToTime: [
            ['createTime', ['createTimeStart', 'createTimeEnd'], 'YYYY-MM-DD'],
            ['departTime', ['departTimeStart', 'departTimeEnd'], 'YYYY-MM-DD'],
            ['finishTime', ['finishTimeStart', 'finishTimeEnd'], 'YYYY-MM-DD'],
          ],
        },
        pagination: { pageSize: 10 },
        expandRowByClick: true,
        striped: true,
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: true,
        tableSetting: { fullScreen: true },
      });
      function customHeader() {
        let header = {};
        let orderArr: object[] = [];
        for (let i = 0; i < getBasicColumns().length; i++) {
          // console.log(getBasicColumns()[i].title);
          let key: string = (getBasicColumns()[i].dataIndex as string) ?? '';
          header[key] = getBasicColumns()[i].title;
        }
        header['id'] = '序号';
        console.log(toRaw(getDataSource()));
        for (let k = 0; k < toRaw(getDataSource()).length; k++) {
          let data = ref({
            id: 1,
            a0: '终端类型',
            a1: '预订时间',
            a2: '预订人',
            a3: '出差申请单号',
            a4: '订单号',
            a5: '员工编号',
            a6: '乘车人',
            a7: '乘车人电话',
            a8: '上车地点',
            a9: '下车地点',
            a10: '用车时间',
            a70: '抵达时间',
            a11: '车型等级',
            a12: '供应商渠道',
            a13: '出差人数',
            a14: '车费',
            a15: '深白云服务费',
            a16: '售票处服务费',
            a17: '深白云扣款',
            a18: '应收总计',
            a19: '总金额包含服务费',
            a20: '车辆信息',
            a21: '订单类型',
            a22: '服务商编码',
            a23: '客户名称',
            a24: '客户编号',
            a25: '收款状态',
            a26: '结算状态',
            a27: '联系人',
            a28: '联系人电话',
            a29: '成本中心',
            a30: 'OA申请单号',
            a31: '业务经理',
            a32: '服务商名称',
            a33: '服务费',
            a34: '供应商单号',
            a35: '订单来源',
            a36: '预订人部门',
            a37: '支付状态',
            a38: '支付方式',
            a39: '个人支付金额',
            a40: '个人退款金额',
            a41: '公司支付金额',
            a42: '公司退款金额',
            a43: '交易流水号',
            a44: '费用承担部门',
            a45: '项目编号',
            a46: '项目名称',
            a47: '差旅事项',
            a48: '超标原因',
            a49: '超标金额',
            a50: '超标描述',
            a51: '费用类型',
            a52: '差旅授权号',
            a53: '审批状态',
            a54: '给审批人留言',
            a55: '审批组1类型',
            a56: '审批组1审批人',
            a57: '审批组2类型',
            a58: '审批组2审批人',
            a59: '审批组3类型',
            a60: '审批组3审批人',
            a61: '审批组4类型',
            a62: '审批组4审批人',
            a63: '员工部门',
            a64: '消费记录id（唯一值）',
            // a65: '订单号',
            // a66: '出发时间',
            // a67: '到达时间',
            a68: '乘车人卡号',
          });
          let record = getDataSource()[k];
          data.value.id = k + 1;
          data.value.a0 = 'app';
          data.value.a1 = record.createTime
            ? dayjs(record.createTime).format('YYYY-M-D HH:mm:ss')
            : '-';
          data.value.a2 = record.reserveName;
          data.value.a3 = record.externalApprovalNo ? record.externalApprovalNo : '-';
          data.value.a4 = record.orderNo;
          data.value.a5 = record.accountNo;
          data.value.a6 = record.orderDetailDto.passengerList[0].name;
          data.value.a7 = record.orderDetailDto.passengerList[0].phone;
          data.value.a8 = record.departCityName + '-' + record.departAddress;
          data.value.a9 = record.arriveCityName + '-' + record.arriveAddress;
          data.value.a10 = record.departureTime
            ? dayjs(record.departureTime).format('YYYY-MM-DD HH:mm:ss')
            : '-';
          if (record.carCategory === 'COMFORT') data.value.a11 = '舒适型';
          if (record.carCategory === 'ECONOMIC') data.value.a11 = '经济型';
          // data.value.a11 = record.carCategory;
          data.value.a12 = record.orderDetailDto.childChannelName
            ? record.orderDetailDto.childChannelName
            : '-';
          data.value.a13 = record.orderDetailDto.passengerList.length;
          data.value.a14 = record.realFee;
          data.value.a15 = '0';
          data.value.a16 = '0';
          data.value.a17 = record.realFee;
          data.value.a18 = record.realFee;
          data.value.a19 = record.realFee;
          data.value.a20 = record.orderDetailDto.driver
            ? record.orderDetailDto.driver.licensePlate
            : '-';
          data.value.a21 = '有审批单';
          data.value.a22 = record.orderDetailDto.proxyId;
          data.value.a23 = record.orderDetailDto.passengerList[0].name;
          data.value.a24 = record.accountNo;
          data.value.a25 = record.payStatus === 'UNPAID' ? '未支付' : '已支付';
          data.value.a26 = '-';
          data.value.a27 = record.reserveName;
          data.value.a28 = record.orderDetailDto.passengerList[0].phone;
          data.value.a29 = '-';
          data.value.a30 = record.externalApprovalNo ? record.externalApprovalNo : '-';
          data.value.a31 = '-';
          data.value.a32 = '-';
          data.value.a33 = '0';
          data.value.a34 = record.orderDetailDto.channelOrderNo
            ? record.orderDetailDto.channelOrderNo
            : '-';
          data.value.a35 = '-';
          data.value.a36 = record.examineApprove ? record.examineApprove.departmentId : '-';
          data.value.a37 = '已支付';
          data.value.a38 = '月结';
          data.value.a39 = '0';
          data.value.a40 = '0';
          data.value.a41 = record.realFee;
          data.value.a42 = '0';
          data.value.a43 = '-';
          data.value.a44 = '-';
          data.value.a45 = '-';
          data.value.a46 = '-';
          data.value.a47 = '-';
          data.value.a48 = '-';
          data.value.a49 = '0';
          data.value.a50 = '-';
          data.value.a51 = '-';
          data.value.a52 = '-';
          data.value.a53 = '-';
          data.value.a54 = '-';
          data.value.a55 = '-';
          data.value.a56 = '-';
          data.value.a57 = '-';
          data.value.a58 = '-';
          data.value.a59 = '-';
          data.value.a60 = '-';
          data.value.a61 = '-';
          data.value.a62 = '-';
          data.value.a63 = record.examineApprove ? record.examineApprove.departmentId : '-';
          data.value.a64 = record.id;
          data.value.a70 = record.finishTime
            ? dayjs(record.finishTime).format('YYYY-MM-DD HH:mm:ss')
            : '-';
          // data.value.a65 = record.orderNo;
          // data.value.a66 = record.departureTime
          //   ? dayjs(record.departureTime).format('YYYY/MM/DD HH:mm:ss')
          //   : '-';
          // data.value.a67 = record.finishTime
          //   ? dayjs(record.finishTime).format('YYYY/MM/DD HH:mm:ss')
          //   : '-';
          data.value.a68 = record.orderDetailDto.passengerList[0].certNo
            ? record.orderDetailDto.passengerList[0].certNo
            : '-';
          orderArr.push(data.value);
        }

        jsonToSheetXlsx({
          data: orderArr,
          header,
          filename: '专车订单统计' + dayjs(new Date()).format('YYYY-MM-DD') + '.xlsx',
          json2sheetOpts: {
            // 指定顺序
            header: [
              'id',
              'a0',
              'a1',
              'a2',
              'a3',
              'a4',
              'a5',
              'a6',
              'a7',
              'a8',
              'a9',
              'a10',
              'a70',
              'a11',
              'a12',
              'a13',
              'a14',
              'a15',
              'a16',
              'a17',
              'a18',
              'a19',
              'a20',
              'a21',
              'a22',
              'a23',
              'a24',
              'a25',
              'a26',
              'a27',
              'a28',
              'a29',
              'a30',
              'a31',
              'a32',
              'a33',
              'a34',
              'a35',
              'a36',
              'a37',
              'a38',
              'a39',
              'a40',
              'a41',
              'a42',
              'a43',
              'a44',
              'a45',
              'a46',
              'a47',
              'a48',
              'a49',
              'a50',
              'a51',
              'a52',
              'a53',
              'a54',
              'a55',
              'a56',
              'a57',
              'a58',
              'a59',
              'a60',
              'a61',
              'a62',
              'a63',
              'a64',
              // 'a66',
              // 'a67',
              'a68',
            ],
          },
        });
      }
      return {
        registerTable,
        customHeader,
      };
    },
  });
</script>
